<?php
class PostSettingAction extends SystemAction {
	protected $tplattrs = array (
			'sendername' => '发件人姓名',
			'sendercom' => '发件人单位',
			'senderaddr' => '发件人地址',
			'sendermobile' => '发件人手机',
			'senderphone' => '发件人电话',
			'sendzip' => '发件人邮编',
			'buyer_nick' => '收件人昵称',
			'receiver_name' => '收件人姓名',
			'receiver_state' => '收件人省份',
			'receiver_city' => '收件人城市',
			'receiver_district' => '收件人县区',
			'receiver_address' => '收件人详细地址',
			'receiver_mobile' => '收件人手机',
			'receiver_phone' => '收件人电话',
			'receiver_zip' => '收件人邮编',
			'productinfo' => '商品信息',
			'seller_memo' => '卖家备注',
			'seller_nick'	=> '卖家昵称',
			'printdate' => '打印日期',
			'diy1' => '自定义内容一',
			'diy2' => '自定义内容二',
			'diy3' => '自定义内容三'
	);
	function companylist() {
		$companys = D ( 'Postsetting' )->getCompanys ();
		$this->assign ( 'companys', $companys );
		$this->display ();
	}
	function companyedit() {
		$this->display ();
	}
	/**
	 * 模板列表
	 */
	function templatelist() {
		$tpllist = D ( 'Postsetting' )->getTemplates ( '1' );
		$this->assign ( 'tpllist', $tpllist );
		$this->display ();
	}
	function templateadd() {
		$ps = D ( 'Postsetting' );
		$companys = $ps->viewCompanys ();
		$district = $ps->viewDisctrict ();
		$this->assign ( 'companys', $companys );
		$this->assign ( 'district', $district );
		$this->display ();
	}
	function templatesave() {
		$pt = D ( "Posttemplate" );
		
		if ($pt->create ()) {
			if (false !== $pt->add ()) {
				$tplid = $pt->getLastInsID ();
				// 保存地区价格设置
				$dids = $_POST ['did'];
				$ptdstartprice = $_POST ['ptdstartprice'];
				$ptdincreaseprice = $_POST ['ptdincreaseprice'];
				
				$ptd = array ();
				foreach ( $dids as $k => $did ) {
					$ptd [$k] ['tplid'] = $tplid;
					$ptd [$k] ['did'] = $did;
					$ptd [$k] ['dname'] = D ( 'Postsetting' )->getDiscrictName ( $did );
					$ptd [$k] ['ptdstartprice'] = $ptdstartprice [$k];
					$ptd [$k] ['ptdincreaseprice'] = $ptdincreaseprice [$k];
				}
				
				$ptdModel = D ( 'Posttemplatedistrict' );
				$ptdModel->addAll ( $ptd );
				
				$this->ajaxReturn ( '', '数据添加成功！', 1 );
			} else {
				$this->error ( '数据写入错误' );
			}
		} else {
			$this->error ( $pt->getError () );
		}
	}
	function templateedit() {
		$tplid = intval ( $_GET ['tplid'] );
		$tplitem = D ( 'PosttemplateView' )->where ( "Posttemplate.id={$tplid}" )->find ();
		if (! $tplitem) {
			$this->error ( 'NOtFOUND', U ( 'PostSetting/templatelist' ) );
		}
		$this->assign ( 'tplitem', $tplitem );
		
		$ps = D ( 'Postsetting' );
		$companys = $ps->viewCompanys ();
		$district = $ps->viewDisctrict ();
		$this->assign ( 'companys', $companys );
		$this->assign ( 'district', $district );
		
		$this->display ();
	}
	function templateeditsave() {
		$pt = D ( "Posttemplate" );
		if ($vo = $pt->create ()) {
			$list = $pt->save ();
			if ($list !== false) {
				$this->ajaxReturn ( $vo, '数据更新成功！', 1 );
			} else {
				$this->error ( "没有更新任何数据!" );
			}
		} else {
			$this->error ( $pt->getError () );
		}
		
		$this->display ();
	}
	function templateprice() {
		$tplid = intval ( $_GET ['tplid'] );
		$ps = D ( 'Postsetting' );
		$ptd = D ( 'Posttemplatedistrict' );
		
		$companys = $ps->viewCompanys ();
		$district = $ps->viewDisctrict ();
		
		$tplist = $ptd->where ( "tplid={$tplid}" )->order('ptdstartprice ASC, did asc ')->select ();
		 
		$distr = '';
		if ($tplist) {
			foreach ( $tplist as $to ) {
				$distr .= '<div id="tplist_'.$to['id'].'">';
				$distr .= '到达地区:<select name="did[]"> ';
				$distr .= $ps->viewDisctrict ( $to ['did'] );
				$distr .= '</select>';
				$distr .= ' 起步价:<input name="ptdstartprice[]" type="text" id="qbj" value="' . $to ['ptdstartprice'] . '" size="10" />';
				$distr .= ' 加价:<input name="ptdincreaseprice[]" type="text" id="dqjj" value="' . $to ['ptdincreaseprice'] . '" size="10" />';
				$distr .= ' <input type="button" name="button" id="btn_delprice2" value="删除" alt='.$to['id'].' />';
				$distr .= '</div>';
			}			
			
		}
		
		$this->assign ( 'distr', $distr );
		$this->assign ( 'tplid', $tplid );
		$this->assign ( 'companys', $companys );
		$this->assign ( 'district', $district );
		
		$this->display ();
	}
	function templatepricesave() {
		$tplid = intval($_POST['tplid']);
		$ptd = D ( 'Posttemplatedistrict' );
		foreach( $_POST['did'] as $k=>$tp ) {
			if( $_POST['ptdstartprice'][ $k ] && $_POST['ptdincreaseprice'][ $k ]) {
				$data = array();
				$data['did'] = $tp;
				$data['tplid'] = $tplid;
				$data['dname'] =  D ( 'Postsetting' )->getDiscrictName ( $tp );
				$data['ptdstartprice'] = $_POST['ptdstartprice'][ $k ];
				$data['ptdincreaseprice'] = $_POST['ptdincreaseprice'][ $k ];
				$find = $ptd->where("tplid={$tplid} AND did={$tp}")->find();
				if( $find ) {
					$vo[] = $ptd->where("tplid={$tplid} AND did={$tp}")->save( $data );
				}else {
					$vo[] = $ptd->add( $data );
				}
			}
		}
		if ($vo) {
			$this->ajaxReturn ( $vo, '保存成功', 1 );
		} else {
			$this->error ( $ptd->getError () );
		}
	}
	function templatepricedel(){
		$id = intval( $_POST['id'] );
		$tplid = intval( $_POST['tplid'] );
		$ptd = D ( 'Posttemplatedistrict' );
		$vo = $ptd->where("id={$id} AND tplid={$tplid}")->delete();
		if( $vo ) {
			$this->ajaxReturn($vo, '删除成功', 1);
		}else {
			$this->error( $ptd->getError() );
		}
	}
	function tplcontent() {
// 		$tplattrs = array (
// 				'sendername' => '发件人姓名',
// 				'sendercom' => '发件人单位',
// 				'senderaddr' => '发件人地址',
// 				'sendermobile' => '发件人手机',
// 				'senderphone' => '发件人电话',
// 				'sendzip' => '发件人邮编',
// 				'receivername' => '收件人姓名',
// 				'receiverprovince' => '收件人省份',
// 				'receivercity' => '收件人城市',
// 				'receivercountry' => '收件人县区',
// 				'receiverstreet' => '收件人街道地址',
// 				'receiveraddress' => '收件人详细地址',
// 				'receivermobile' => '收件人手机',
// 				'receiverphone' => '收件人电话',
// 				'receiverzip' => '收件人邮编',
// 				'productinfo' => '商品信息',
// 				'sellerremark' => '卖家备注',
// 				'printdate' => '打印日期',
// 				'diy1' => '自定义内容一',
// 				'diy2' => '自定义内容二',
// 				'diy3' => '自定义内容三' 
// 		);
		$tplcssname = array (
				'marginleft',
				'margintop',
				'fontsize',
				'width' 
		);
		$tpl = D ( 'Posttemplate' )->where ( "id={$_GET['tplid']}" )->find ();
		
		if (! $tpl) {
			$this->error ( 'NOT FOUND', U ( 'PostSetting/templatelist' ) );
		}
		$this->assign ( 'tplid', $_GET ['tplid'] );
		$this->assign ( 'tplattrs', $this->tplattrs );
		$this->assign ( 'tplcssname', $tplcssname );
		$this->assign( 'tpl', $tpl );
		$this->assign ( 'tplcontent', unserialize ( $tpl ['tplcontent'] ) );
		$this->display ();
	}
	/**
	 * 保存打印模板配置
	 */
	function tplcontentsave() {
		$pt = D ( "Posttemplate" );
		$id = intval ( $_POST ['id'] );
		foreach ( $_POST as $k => $v ) {
			if (false !== strpos ( $k, '-' )) {
				$tplcontent [$k] = $v;
			}
		}
		$data ['tplwidth'] = intval( $_POST[tplwidth] );
		$data ['tplheight'] = intval( $_POST[tplheight] );
		$data ['tplleft'] = intval( $_POST[tplleft] );
		$data ['tpltop'] = intval( $_POST[tpltop] );
		$data ['tplcontent'] = serialize ( $tplcontent );
		$vo = $pt->where ( "id={$id}" )->save ( $data );
		if ($vo) {
			$this->ajaxReturn ( $vo, '保存成功', 1 );
		} else {
			$this->error ( $pt->getError ());
		}
	}
	function ttt() {
		echo D ( 'Postsetting' )->getDiscrictName ( 9 ) ;
		echo D('Postsetting')->_sql();
		 
	}
}